import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Pageable, SparrowUrl, SysroleUrlPermissionPK } from '../domain/sparrow';

@Injectable({
  providedIn: 'root'
})
export class SparrowAppService {
  host:string = "http://localhost:8091/api";

  constructor(private httpClient: HttpClient) { }

  public list(pageable: Pageable | any){
    let p:Pageable|any = {};
    if(pageable!=null){
      p.page=pageable.page -1;
      p.size = pageable.size;
      if(pageable.sort)
        p.sort = pageable.sort;
    }else{

    }
    p = undefined
    return this.httpClient.get<any>(this.host + "/apps", {params:p});
  }

  public save(urls: SparrowUrl[]){
    return this.httpClient.post<any>(this.host + "/apps/batch", urls);
  }

  /**
   * name
   */
  public delete(ids: string[]|any) {
    console.log(ids)
    return this.httpClient.delete<any>(this.host + "/apps/batch", {body:ids});
  }

  public getPermissionById(ids: string[]|null|any){
    return this.httpClient.post<any>(this.host + "/apps/getPermissionById", ids);
  }

  public addPermission(ids:SysroleUrlPermissionPK[]){
    return this.httpClient.post<any>(this.host + "/apps/permissions", ids);
  }

  public delPermission(ids:SysroleUrlPermissionPK[]|any){
    return this.httpClient.delete<any>(this.host + "/apps/permissions", {body:ids});
  }
}
